//
//  HBMyBankAccountCtrl.m
//  HuoBao
//
//  Created by chaiyanan on 15/12/7.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBMyBankAccountCtrl.h"
#import "HBMyBankReleaseCtrl.h"
#import "HBGetUserStatusManager.h"
#import "HBIdentifierCheckCtrl.h"
#import "HBAddBankCtrl.h"

#import "HBListBankCardInfo.h"
#import "HBListBankCardItemInfo.h"
#import "HBListBankCardParam.h"
#import "HBListBankCardParser.h"

#import "HBDisbindBankCardInfo.h"
#import "HBDisbindBankCardParam.h"
#import "HBDisbindBankCardParser.h"

#import "HBUserDistillParam.h"
#import "HBUserDistillParser.h"
#import "HBUserDistillInfo.h"

#import "CustomReleaseBankBindAlert.h"
#import "HBCompleteBankInfoCtrl.h"
#import "HBWithdrawCtrl.h"

#import "CustomNoTradingPwdAlert.h"
#import "HBSetTradpasswordCtrl.h"

#define leftMargan  (IsIphone4?15:(IsIphone5?15:(IsIphone6?15:15)))

#define BankAccountBGColor1     @"842525"
#define BankAccountTypeColor1   @"c48383"
#define BankAccountBGColor2     @"009173"
#define BankAccountTypeColor2   @"84dbc2"
#define BankAccountBGColor3     @"1c4581"
#define BankAccountTypeColor3   @"6e8bc9"
#define BankAccountBGColor4     @"62187a"
#define BankAccountTypeColor4   @"a776bc"
#define BankAccountBGColor5     @"1ba2d3"
#define BankAccountTypeColor5   @"9edff7"

@interface HBMyBankAccountCtrl ()

@property (nonatomic, strong) UILabel *statusLable;
@property (nonatomic, strong) UIButton *releaseBindBtn;

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *bankImageView;
@property (nonatomic, strong) UILabel *bankNameLable;
@property (nonatomic, strong) UILabel *bankTypeLable;
@property (nonatomic, strong) UILabel *bankAccountLable;
@property (nonatomic, strong) UILabel *desLable;

@property (nonatomic, strong) HBListBankCardItemInfo *cardInfo;

@property (nonatomic, strong) NSArray *bankAry1;
@property (nonatomic, strong) NSArray *bankAry2;
@property (nonatomic, strong) NSArray *bankAry3;
@property (nonatomic, strong) NSArray *bankAry4;
@property (nonatomic, strong) NSArray *bankAry5;

@property (nonatomic, strong) UIImageView *emptyIV;


@end


@implementation HBMyBankAccountCtrl


#pragma mark -
#pragma mark - system

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.bankAry1 = @[@"CIB",@"CMB",@"BOC",@"ICBC",@"CITIC",@"GDB",@"PAB",@"HXB"];
    self.bankAry2 = @[@"ABC"];
    self.bankAry3 = @[@"CCB",@"BCOM",@"CMB",@"SPDB"];
    self.bankAry4 = @[@"CEB"];
    self.bankAry5 = @[@"CMBC"];
}

- (void)buildLayout
{
    [super buildLayout];
    self.title = @"我的银行卡";
    
    self.statusLable = [self createLableWithText:@"已绑定的银行卡" font:[UIFont systemFontOfSize:18.0f] color:[LCCommon getColor:@"b1b1b1"] textAlignment:NSTextAlignmentCenter frame:CGRectMake(leftMargan, 0, SCREEN_WIDTH-2*(leftMargan), 45) superView:self.view];
    
    //    UIView *topBG =
    self.bgView = [self createSepratorLineWithFrame:CGRectMake(leftMargan, 45, SCREEN_WIDTH-2*(leftMargan), 110) backgroundColor:[LCCommon getColor:@"842525"] superView:self.view];
    self.bgView.layer.cornerRadius = 4.0f;
    self.bgView.clipsToBounds = YES;
    
    self.bankImageView = [[UIImageView alloc]initWithFrame:CGRectMake(17, 15, 35, 35)];
    self.bankImageView.image = [UIImage imageNamed:@"CMB_Bind"];
    [self.bgView addSubview:self.bankImageView];
    
    self.bankNameLable = [self createLableWithText:@"招商银行" font:[UIFont systemFontOfSize:18.0f] color:[LCCommon getColor:@"ffffff"] textAlignment:NSTextAlignmentLeft frame:CGRectMake(self.bankImageView.x+self.bankImageView.width+12, 15, 200, 18) superView:self.bgView];
    self.bankTypeLable = [self createLableWithText:@"存储卡" font:[UIFont systemFontOfSize:12.0f] color:[LCCommon getColor:@"c48383"] textAlignment:NSTextAlignmentLeft frame:CGRectMake(self.bankImageView.x+self.bankImageView.width+12, self.bankNameLable.y+self.bankNameLable.height+5, 200, 12) superView:self.bgView];
    
    self.bankAccountLable = [self createLableWithText:@"*** **** **** 2457" font:[UIFont systemFontOfSize:18.0f] color:[LCCommon getColor:@"ffffff"] textAlignment:NSTextAlignmentLeft frame:CGRectMake(self.bankNameLable.x, 73, self.bgView.width-(self.bankNameLable.x), 18) superView:self.bgView];
    self.bankAccountLable.contentMode = UIViewContentModeCenter;
    
    
    self.desLable = [self createLableWithText:@"提示：解绑当前银行卡需将目前投资总额结算后，才能解除绑定银行卡" font:[UIFont systemFontOfSize:14.0f] color:[LCCommon getColor:@"777777"] textAlignment:NSTextAlignmentLeft frame:CGRectMake(leftMargan, self.bgView.y+self.bgView.height+15, SCREEN_WIDTH-2*leftMargan, 40) superView:self.view];
    self.desLable.numberOfLines = 0;
    [self.desLable sizeToFit];
    CGRect lableFrame = self.desLable.frame;
    lableFrame.origin = CGPointMake(leftMargan, self.bgView.y+self.bgView.height+15);
    self.desLable.frame = lableFrame;
    
    
    
    self.releaseBindBtn = [self createButtonWithFrame:CGRectMake(leftMargan, SCREEN_HEIGHT-64-44-35, SCREEN_WIDTH-2*(leftMargan), 44) normalBackgroundColor:[LCCommon getColor:@"f24545"] disabledBackgroundColor:[LCCommon getColor:@"bd3737"] higlightedBackgroundColor:[LCCommon getColor:@"d43333"] normalFontColor:[LCCommon getColor:@"ffffff"] higlightedFontColor:[LCCommon getColor:@"ffffff"] disabledFontColor:[LCCommon getColor:@"fd7a7a"] title:@"解除绑定" contentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter font:[UIFont systemFontOfSize:18] superView:self.view];
    self.releaseBindBtn.enabled = YES;
    
    [self reloadViewType];
    
    if (self.bindType == MyBankBindType_None)
    {
        self.statusLable.text = @"未绑定银行卡";
        self.bgView.alpha = 0;
        self.desLable.alpha = 0;
        [self.releaseBindBtn setTitle:@"+ 添加银行卡" forState:UIControlStateNormal];
    }
    else
    {
        self.statusLable.text = @"已绑定的银行卡";
        self.bgView.alpha = 1;
        self.desLable.alpha = 1;
        [self.releaseBindBtn setTitle:@"解除绑定" forState:UIControlStateNormal];
        [self requestToGetBankInfo];
    }
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    if (self.bindType == MyBankBindType_None)
    {
        self.statusLable.text = @"未绑定银行卡";
        self.bgView.alpha = 0;
        self.desLable.alpha = 0;
        [self showEmptyIV];
        [self.releaseBindBtn setTitle:@"+ 添加银行卡" forState:UIControlStateNormal];
    }
    else
    {
        [self hideEmptyIV];
    }
}



#pragma mark -
#pragma mark - private

- (void)showEmptyIV
{
    self.emptyIV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"My_Bank_Empty"]];
    self.emptyIV.center = CGPointMake(SCREEN_WIDTH/2.0, SCREEN_HEIGHT/2.0);
    self.emptyIV.center = CGPointMake(self.emptyIV.center.x, self.emptyIV.center.y-32);
    [self.view addSubview:self.emptyIV];
    
}

- (void)hideEmptyIV
{
    if (self.emptyIV)
    {
        [self.emptyIV removeFromSuperview];
        self.emptyIV = nil;
    }
    
 
}

- (void)reloadCardData
{
    /*
     @property (nonatomic, strong) UIView *bgView;
     @property (nonatomic, strong) UIImageView *bankImageView;
     @property (nonatomic, strong) UILabel *bankNameLable;
     @property (nonatomic, strong) UILabel *bankTypeLable;
     @property (nonatomic, strong) UILabel *bankAccountLable;
     */
    self.bankNameLable.text = self.cardInfo.bankName;
    self.bankAccountLable.text = self.cardInfo.bankCardno;
    self.bankImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_Bind",self.cardInfo.bankCode]];
    if ([self.bankAry1 containsObject:self.cardInfo.bankCode])
    {
        self.bgView.backgroundColor = [LCCommon getColor:BankAccountBGColor1];
        self.bankTypeLable.textColor = [LCCommon getColor:BankAccountTypeColor1];
    }
    else if ([self.bankAry2 containsObject:self.cardInfo.bankCode])
    {
        self.bgView.backgroundColor = [LCCommon getColor:BankAccountBGColor2];
        self.bankTypeLable.textColor = [LCCommon getColor:BankAccountTypeColor2];
    }
    else if ([self.bankAry3 containsObject:self.cardInfo.bankCode])
    {
        self.bgView.backgroundColor = [LCCommon getColor:BankAccountBGColor3];
        self.bankTypeLable.textColor = [LCCommon getColor:BankAccountTypeColor3];
    }
    else if ([self.bankAry4 containsObject:self.cardInfo.bankCode])
    {
        self.bgView.backgroundColor = [LCCommon getColor:BankAccountBGColor4];
        self.bankTypeLable.textColor = [LCCommon getColor:BankAccountTypeColor4];
    }
    else if ([self.bankAry5 containsObject:self.cardInfo.bankCode])
    {
        self.bgView.backgroundColor = [LCCommon getColor:BankAccountBGColor5];
        self.bankTypeLable.textColor = [LCCommon getColor:BankAccountTypeColor5];
    }
    
}

- (void)reloadViewType
{
}

- (UILabel *)createLableWithText:(NSString *)text font:(UIFont *)font color:(UIColor *)fontColor textAlignment:(NSTextAlignment)textAlignment frame:(CGRect)frame superView:(UIView *)superView
{
    UILabel *lable = [[UILabel alloc]initWithFrame:frame];
    lable.backgroundColor = [UIColor clearColor];
    lable.text = text;
    lable.font = font;
    lable.textColor = fontColor;
    lable.textAlignment = textAlignment;
    [superView addSubview:lable];
    return lable;
}

- (UIView *)createSepratorLineWithFrame:(CGRect)frame backgroundColor:(UIColor *)bgColor superView:(UIView *)superView
{
    UIView *sepratorLine = [[UIView alloc]initWithFrame:frame];
    sepratorLine.backgroundColor = bgColor;
    [superView addSubview:sepratorLine];
    return sepratorLine;
}

- (UIButton *)createButtonWithFrame:(CGRect)frame normalBackgroundColor:(UIColor *)normalBGColor disabledBackgroundColor:(UIColor *)disabledBGColor higlightedBackgroundColor:(UIColor *)higlightedBGColor  normalFontColor:(UIColor *)normalFontColor higlightedFontColor:(UIColor *)higlightedFontColor disabledFontColor:(UIColor *)disabledFontColor title:(NSString *)title contentHorizontalAlignment:(UIControlContentHorizontalAlignment)textAlignment font:(UIFont *)font superView:(UIView *)superView
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setBackgroundColor:normalBGColor];
    [button setBackgroundColor:normalBGColor forState:UIControlStateNormal];
    [button setBackgroundColor:disabledBGColor forState:UIControlStateDisabled];
    [button setBackgroundColor:higlightedBGColor forState:UIControlStateHighlighted];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:normalFontColor forState:UIControlStateNormal];
    [button setTitleColor:disabledFontColor forState:UIControlStateDisabled];
    [button setTitleColor:higlightedFontColor forState:UIControlStateHighlighted];
    button.titleLabel.font = font;
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setContentHorizontalAlignment:textAlignment];
    [superView addSubview:button];
    button.enabled = NO;
    [button.layer setCornerRadius:4.0f];
    button.layer.masksToBounds = YES;
    return button;
}


#pragma mark -
#pragma mark - active

- (void)clickButton:(UIButton *)sender
{
    if (sender == self.releaseBindBtn)
    {
        if (self.bindType==MyBankBindType_None)
        {
            if ([HBGetUserStatusManager sharedHBGetUserStatusManager].isAllAuthor)
            {
               
            }
            else
            {
//                [CustomLoadingView showHudToView:nil];
                [CustomLoadingView showHudToView:self.view];
                [HBGetUserStatusManager requestToGetUserStatusWithResult:^(BOOL isSuccess, NSString *msg, NSError *error) {
//                    [CustomLoadingView hideHudForView:nil];
                    [CustomLoadingView hideHudForView:self.view];
                    
                    HBAddBankCtrl *bankCtrl = [[HBAddBankCtrl alloc]initWithNibName:NSStringFromClass([HBAddBankCtrl class]) bundle:nil];
                    bankCtrl.viewpageJumpingFrom = isFromBankView;
                    bankCtrl.isRealName = [[HBGetUserStatusManager sharedHBGetUserStatusManager].userStatusInfo.identityStatus integerValue];
                    [self.navigationController pushViewController:bankCtrl animated:YES];
                    
                }];
            }
        }
        else
        {
            //解除绑定
//            HBMyBankReleaseCtrl *releaseCtrl = [[HBMyBankReleaseCtrl alloc]init];
//            [self.navigationController pushViewController:releaseCtrl animated:YES];
            [self requestToDisBindBank];
        }
    }
}


#pragma mark -
#pragma mark - request

- (void)requestToGetBankInfo
{
     [CustomLoadingView showHudToView:self.view];
     HBListBankCardParam *param = [[HBListBankCardParam alloc]init];
     param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
     param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
     HBListBankCardParser *parser = [[HBListBankCardParser alloc]init];
     [NetCenter getDataFromURL:Method_listBankCard params:param parser:parser result:^(ParserResult *myResult) {
    [CustomLoadingView hideHudForView:self.view];
     if (myResult.code==0)
     {
         HBListBankCardInfo *listInfo = (HBListBankCardInfo *)myResult.result;
         if (listInfo.dataAry.count>0)
         {
             self.cardInfo = listInfo.dataAry[0];
             [self reloadCardData];
         }
         
     }
     else
     {
         DISSMISS_ERR(myResult.message);
     }
     
     } failure:^(NSError *error) {
     [CustomLoadingView hideHudForView:self.view];
         DISSMISS_ERR([error description]);
     }];
    
}

- (void)requestToDisBindBank
{
    [CustomLoadingView showHudToView:self.view];
    HBDisbindBankCardParam *param = [[HBDisbindBankCardParam alloc]init];
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    param.bankCardId = self.cardInfo.bankCardId;
    HBDisbindBankCardParser *parser = [[HBDisbindBankCardParser alloc]init];
    [NetCenter postDataFromURL:Method_disbindBankCard params:param parser:parser result:^(ParserResult *myResult) {
        [CustomLoadingView hideHudForView:self.view];
        if (myResult.code==0)
        {
            HBDisbindBankCardInfo *listInfo = (HBDisbindBankCardInfo *)myResult.result;
            if ([listInfo.accountStatus integerValue])
            {
                //有余额
               CustomReleaseBankBindAlert *releaseBindAlert = [[CustomReleaseBankBindAlert alloc]init];
                [releaseBindAlert showTradPasswordAlertView];
                __weak typeof(self) weakSelf = self;
                releaseBindAlert.ActionCustomReleaseBank = ^(CustomReleaseBankBindAlert *bindAlert){
                    [bindAlert removeFromSuperview];
                    [weakSelf pressToDepo];
                };
            }
            else
            {
                [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%f",[NSDate date].timeIntervalSince1970 + 60] forKey:RELEASEBANK_CODE_TIME];
                [[NSUserDefaults standardUserDefaults] synchronize];
                
                HBMyBankReleaseCtrl *bankReleaseCtrl = [[HBMyBankReleaseCtrl alloc]init];
                bankReleaseCtrl.phoneBind = listInfo.mobile;
                bankReleaseCtrl.bankCardId = self.cardInfo.bankCardId;
                bankReleaseCtrl.bankAccountCtrl = self;
                [self.navigationController pushViewController:bankReleaseCtrl animated:YES];
            }
            
        }
        else
        {
            DISSMISS_ERR(myResult.message);
        }
        
    } failure:^(NSError *error) {
        [CustomLoadingView hideHudForView:self.view];
        DISSMISS_ERR([error description]);
    }];
    
}

-(void)pressToDepo{
    //    HBCompleteBankInfoCtrl *draw = [[HBCompleteBankInfoCtrl alloc]init];
    //    [self.navigationController pushViewController:draw animated:YES];
    
//    [CustomLoadingView showHudToView:nil];
    [CustomLoadingView showHudToView:self.view];
    HBUserDistillParam *param = [[HBUserDistillParam alloc]init];
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    HBUserDistillParser *parser = [[HBUserDistillParser alloc]init];
    [NetCenter postDataFromURL:Method_userDistill params:param parser:parser result:^(ParserResult *myResult) {
//        [CustomLoadingView hideHudForView:nil];
        [CustomLoadingView hideHudForView:self.view];
        if (myResult.code == 0)
        {
            HBUserDistillInfo *info = (HBUserDistillInfo *)myResult.result;
            
            if(![info.accountBankStatus  integerValue])
            {
                HBCompleteBankInfoCtrl *draw = [[HBCompleteBankInfoCtrl alloc]init];
                draw.userDistillInfo = info;
                [self.navigationController pushViewController:draw animated:YES];
            }
            else if (![info.tradingPwdStatus integerValue])
            {
                CustomNoTradingPwdAlert *alert = [[CustomNoTradingPwdAlert alloc]init];
                [alert showTradPasswordAlertView];
                alert.ActionClickToSetTradingPwd = ^(CustomNoTradingPwdAlert *alertP)
                {
                    [alertP removeFromSuperview];
                    HBSetTradpasswordCtrl *addBankCtrl = [[HBSetTradpasswordCtrl alloc]initWithNibName:NSStringFromClass([HBSetTradpasswordCtrl class]) bundle:nil];
                    addBankCtrl.isFromTiXian = YES;
                    [self.navigationController pushViewController:addBankCtrl animated:YES];
                };
            }
            else
            {
                HBWithdrawCtrl *expofCtrl = [[HBWithdrawCtrl alloc]init];
                expofCtrl.userDistillInfo = info;
                [self.navigationController pushViewController:expofCtrl animated:YES];
            }
        }
        else
        {
            DISSMISS_ERR(myResult.message);
        }
        
    } failure:^(NSError *error) {
//        [CustomLoadingView hideHudForView:nil];
        [CustomLoadingView hideHudForView:self.view];
        DISSMISS_ERR([error description]);
    }];
    
}




@end
